package com.hsnn.medstgmini.pro.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.annotations.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.pro.base.service.BaseProduceManager;
import com.hsnn.medstgmini.pro.project.model.BidProjCompany;
import com.hsnn.medstgmini.pro.project.service.BidProjCompanyManager;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/proBaseProduce")
public class BaseProduceController {

	@Source
	private BaseProduceManager baseProduceManager;
	
	@Autowired
	private BidProjectManager bidProjectManager;
	
	@Autowired
	private  HttpServletRequest request;
	
	@Autowired
	public void setBaseProduceManager(BaseProduceManager baseProduceManager) {
		this.baseProduceManager = baseProduceManager;
	}

	@Autowired
	private BidProjCompanyManager bidProjCompanyManager;

	/**
	 * @category 跳转到生产 配送企业列表页面
	 * @author 韩守松
	 * @date 2015年6月15日
	 * @param @return
	 */
	@RequestMapping(value = "/toListJsp", method = { RequestMethod.GET, RequestMethod.POST })
	public String getBaseProduceJsp(Model model) {
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO); 
		
		String hospId=null;
		if (user.getType() == 2) {//生产企业
			hospId = ((StdCompinfo)user.getInfo()).getCompId();
		} else if (user.getType() == 3) {//配送企业
			hospId =((StdCompinfo)user.getInfo()).getCompId();
		}
		model.addAttribute("projList", bidProjectManager.getBidProjectListByCompId(hospId, user.getType().toString()));
		return "/pro/base/bidProduceCheckList";
	}

	/**
	 * 配送企业目录管理
	 * 
	 * 用来封装json
	 * 
	 * @param request
	 * @param result
	 * @return
	 */
	@RequestMapping("/getDataList")
	@ResponseBody
	public Pagination getDataList(HttpServletRequest request, BidProjCompany bidProjCompany) {
		Pagination page = new Pagination(request);
		String compId = null;
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		
		if (user.getType() == 2 || user.getType() == 3) {  //生产企业，配送企业
			compId = ((StdCompinfo)user.getInfo()).getCompId();
		} else if (user.getType() == 4) {  //医疗机构
			compId = ((HospInfo)user.getInfo()).getHospId();
		}
		
		String projId = request.getParameter("projId");
		String compName = request.getParameter("compName");
        String compCode = request.getParameter("compCode");
		
		page.getConditions().put("projId", projId);
		page.getConditions().put("compName", compName);
		page.getConditions().put("compId", compId);
		page.getConditions().put("compCode", compCode);
		
		try {
			page = baseProduceManager.getViewGoodsList(page);
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error error) {
			error.printStackTrace();
		}
		return page;
	}
}